//获取应用实例
const app = getApp()
const request = require('../../../../../utils/request');
Page({
    //初始化数据
    data: {
      location:'',
      address: '',
    },
    //获取链接参数
    onLoad: function (e) {
      
      // this.setData({
      //   money: e.money
      // })
    },
    onReady: function(){
        var that = this
        wx.setNavigationBarTitle({
            title: '添加地址'
          })
      //获取缓存地址
      that.setData({
        location: wx.getStorageSync("address")
      })
      if (that.data.location == ''){
        //设置默认值
        that.setData({
          location:'请输入地址'
        })  
      }
    },
    //获取位置信息
    getAddress(){  
    },
    //获取表单信息
    addAdd(e){
      var fromData = e.detail.value
      var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
      if(!myreg.test(fromData.mobile)){
        wx.showModal({
            title: '提示',
            content: '请输入正确的手机号码',
            showCancel: false,
        })
      }else{
        request.HttpRequst(true, 'user/addAddress', false, '',{ "address":this.data.location,"mobile": fromData.mobile,"address_detail": fromData.address_detail,"name": fromData.name,"token": wx.getStorageSync("sessionId")}, "post", false, res =>{
            console.log(res)
            if (res.code==200){
              wx.navigateBack({     //返回上一页面或多级页面
                delta: 1
              })
            }else{
              wx.showModal({
                  title: '提示',
                  content: res.info,
                  showCancel: false
              })
            }
        })
      }
      
    },
    //打开地理位置
    goLocation(){
      // wx.navigateTo({
      //     url: 'location/location'
      // })
      var that = this
      wx.chooseLocation({
        
          success: function(e){
            console.log(e)
              that.setData({
                  location: e.address
              })
          },
          fail: function(e){
              console.log("选择地址失败")
          }
      })
  },
})